/*
When a shadow falls across your world,
and your enemy wants nothing but your annihilation.
Where will you find your allies,
in a desperate struggle for survival.
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m,A[100005],c[100005],a[100005],T,pw[100005],x[4];
char s[100005];
struct node{
    int l,r,lz,sum;
    vector<int> pos;
}tree[400005];
struct query{
    int c,id;
}q[100005];
vector<int> lz[32];
void build(int l,int r,int p,int floor){
    if(!floor)return;
    tree[p].l=l,tree[p].r=r;
    tree[p].lz=lz[floor][l/pw[floor]];
    int mid=tree[p].l+tree[p].r>>1;
    build(l,mid,p*2,floor-1);
    build(mid+1,r,p*2+1,floor-1);
}
void fight(int mx,int rmx,int p,int floor){
    tree[p].pos.clear();
    tree[p].sum=0;
    if(tree[p].l>rmx)return;
    if(floor==1){
        if(tree[p].l>mx)tree[p].sum=tree[p].l+tree[p].r;
        else if(tree[p].lz==0){
            if(a[tree[p].l]>=floor)tree[p].pos.push_back(tree[p].l);
            else if(tree[p].r>mx)tree[p].sum=tree[p].r;
            else tree[p].pos.push_back(tree[p].r);
        }
        else{
            if(tree[p].r>mx)tree[p].sum+=tree[p].r,tree[p].pos.push_back(tree[p].l);
            else if(a[tree[p].r]>=floor)tree[p].pos.push_back(tree[p].r);
            else tree[p].pos.push_back(tree[p].l);
        }
        return;
    }
    fight(mx,rmx,p*2,floor-1);
    fight(mx,rmx,p*2+1,floor-1);
    if(tree[p*2+1].l>rmx){
        tree[p].pos=tree[p*2].pos,tree[p].sum=tree[p*2].sum;
        return;
    }
    if(tree[p].lz==0){
        bool flagwin=0,flaglose=0;
        for(auto u:tree[p*2].pos){
            if(a[u]>=floor)flagwin=1;
            else flaglose=1;
        }
        if(tree[p*2].sum)flagwin=1,flaglose=1;
        if(!flagwin){
            tree[p].pos=tree[p*2+1].pos;
            tree[p].sum=tree[p*2+1].sum;
        }
        else if(!flaglose){
            tree[p].pos=tree[p*2].pos;
            tree[p].sum=tree[p*2].sum;
        }
        else{
            tree[p].pos=tree[p*2+1].pos;
            for(auto u:tree[p*2].pos)if(a[u]>=floor)tree[p].pos.push_back(u);
            tree[p].sum=tree[p*2].sum+tree[p*2+1].sum;
        }
    }
    else{
        bool flagwin=0,flaglose=0;
        for(auto u:tree[p*2+1].pos){
            if(a[u]>=floor)flagwin=1;
            else flaglose=1;
        }
        if(tree[p*2+1].sum)flagwin=1,flaglose=1;
        if(!flagwin){
            tree[p].pos=tree[p*2].pos;
            tree[p].sum=tree[p*2].sum;
        }
        else if(!flaglose){
            tree[p].pos=tree[p*2+1].pos;
            tree[p].sum=tree[p*2+1].sum;
        }
        else{
            tree[p].pos=tree[p*2].pos;
            for(auto u:tree[p*2+1].pos)if(a[u]>=floor)tree[p].pos.push_back(u);
            tree[p].sum=tree[p*2].sum+tree[p*2+1].sum;
        }
    }
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)scanf("%lld",&A[i]);
    for(int i=1;i<=m;i++)scanf("%lld",&c[i]);
    int k=0;
    pw[0]=1;
    while(pw[k]<n)k++,pw[k]=pw[k-1]*2;
    for(int i=1;i<=k;i++){
        scanf("%s",s+1);
        int M=strlen(s+1);
        for(int j=1;j<=M;j++)lz[i].push_back(s[j]-'0');
    }
    build(1,pw[k],1,k);
    scanf("%lld",&T);
    while(T--){
        for(int i=0;i<4;i++)scanf("%lld",&x[i]);
        for(int i=1;i<=n;i++)a[i]=A[i]^x[i%4];
        int ans=0;
        for(int i=1;i<=m;i++){
            if(c[i]==1){
                ans^=i;
                continue;
            }
            int k2=0;
            while(pw[k2]<c[i])k2++;
            fight(c[i],pw[k2],1,k);
            int cur=tree[1].sum;
            for(auto u:tree[1].pos)cur+=u;
            ans^=(i*cur);
        }
        printf("%lld\n",ans);
    }
    return 0;
}